package com.kim.OxBridgeDada.mapper;

import com.kim.OxBridgeDada.model.dto.statistic.AppAnswerCountDTO;
import com.kim.OxBridgeDada.model.dto.statistic.AppAnswerResultCountDTO;
import com.kim.OxBridgeDada.model.entity.UserAnswer;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * @author 风吹落叶红
 * @description 针对表【user_answer(用户答题记录)】的数据库操作Mapper
 * @createDate 2025-04-07 18:08:15
 * @Entity com.kim.OxBridgeDada.model.entity.UserAnswer
 */
public interface UserAnswerMapper extends BaseMapper<UserAnswer> {

    @Select("SELECT appId, COUNT(userId) AS answerCount\n" +
            "FROM (\n" +
            "         SELECT appId, userId FROM user_answer_0\n" +
            "         UNION ALL\n" +
            "         SELECT appId, userId FROM user_answer_1\n" +
            "     ) AS combined\n" +
            "GROUP BY appId\n" +
            "ORDER BY answerCount DESC\n" +
            "LIMIT 10;")
    List<AppAnswerCountDTO> doAppAnswerCount();

    @Select("SELECT resultName, COUNT(resultName) AS resultCount\n" +
            "FROM (\n" +
            "         SELECT resultName\n" +
            "         FROM user_answer_0\n" +
            "         WHERE appId = #{appId}\n" +
            "         UNION ALL\n" +
            "         SELECT resultName\n" +
            "         FROM user_answer_1\n" +
            "         WHERE appId = #{appId}\n" +
            "     ) AS combined\n" +
            "GROUP BY resultName\n" +
            "ORDER BY resultCount DESC;")
    List<AppAnswerResultCountDTO> doAppAnswerResultCount(Long appId);

}




